package com.example.electronicmall.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.adapter.NewsMessageListAdapter;
import com.example.electronicmall.base.SearchMessageActivity;
import com.example.electronicmall.bean.NewsMassageBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
* */
public class NewsFragment extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_news, container, false);
    }

    @Override
    public void onViewCreated(@NonNull  View view, @Nullable  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initciew();
        messageList();
    }

    private <T extends View> T findViewById(@IdRes int resid) {
        return getView().findViewById(resid);
    }
    private void initciew(){
        TextView searchbotton=findViewById(R.id.news_search);//消息跳转
        ImageView addmenu=findViewById(R.id.news_add);//菜单弹框
        searchbotton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               startActivity(new Intent(getContext(), SearchMessageActivity.class));
            }
        });

        addmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              showMenu();
            }
        });
    }

    //菜单弹框
    private void showMenu(){
        final View inflate = LayoutInflater.from(getContext()).inflate(R.layout.news_menu, null);
        Dialog dialog=new AlertDialog.Builder(getContext())
                .setView(inflate)
                .show();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = dm.widthPixels * 5 / 5;
        params.height = dm.heightPixels * 3 / 3;
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    //消息列表
    private void messageList(){
        Date date=new Date();
        RecyclerView recyclerView=findViewById(R.id.news_recycview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        NewsMessageListAdapter adapter;
        List<NewsMassageBean> massageBeanList=new ArrayList<>();
        NewsMassageBean massageBean=new NewsMassageBean();
        for (int i = 0; i <6; i++) {
            massageBean.setNewsicon(String.valueOf(R.mipmap.ic_launcher_round));
            massageBean.setNewstitle("系统消息");
            massageBean.setNewscontext("你有一条订单未支付");
            massageBean.setNewstime(String.valueOf(new SimpleDateFormat("MM:dd:HH:mm:ss").format(date)));
            massageBeanList.add(massageBean);
            adapter=new NewsMessageListAdapter(getContext(),massageBeanList);
            recyclerView.setAdapter(adapter);
        }

    }

}